﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IceDog.WebApi.AnythingApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IceDog.WebApi.AnythingApi.Controllers
{
    /// <summary>
    /// 绑定源参数推断
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class BindingSourceParameterInferenceController : ControllerBase
    {
        /// <summary>
        /// [Get] Get Params From Query
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult GetFromQuery([FromQuery]int id=233,string url="http://www.example.com/q/2019/14323",bool isValid=true)
        {
            return Ok(new {id,url,isValid});
        }

        /// <summary>
        /// [Get] Get Params From Query By Model
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult GetFromQueryByModel([FromQuery]ArgModel arg)
        {
            return Ok(arg);
        }

        /// <summary>
        /// [Post] Get Params From Query
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromQuery([FromQuery]int id = 233, [FromQuery]string url = "http://www.example.com/q/2019/14323", [FromQuery]bool isValid = true)
        {
            return Ok(new { id, url, isValid });
        }

        /// <summary>
        /// [Post] Get Params From Query By Model
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromQueryByModel([FromQuery]ArgModel arg)
        {
            return Ok(arg);
        }

        /// <summary>
        /// [Post] Get Params From Route
        /// </summary>
        /// <returns></returns>
        /// <remarks>通过返回结果可知，`\/` 转换为 `%2f` 就不会重新转换为 `\/`</remarks>
        [HttpPost("[action]/id/{id}/url/{url}/isValid/{isValid}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromRoute([FromRoute]int id = 233, string url = "http://www.example.com/q/2019/14323", bool isValid = true)
        {
            return Ok(new { id, url, isValid });
        }
        /// <summary>
        /// [Post] Get Params From Route By Model
        /// </summary>
        /// <returns></returns>
        /// <remarks>通过返回结果可知，`\/` 转换为 `%2f` 就不会重新转换为 `\/`</remarks>
        [HttpPost("[action]/id/{id}/url/{url}/isValid/{isValid}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromRouteByModel([FromRoute]ArgModel arg)
        {
            return Ok(arg);
        }
        /// <summary>
        /// [Post] Get Params From Body
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromBody([FromBody]ArgModel arg)
        {
            return Ok(arg);
        }

        /// <summary>
        /// [Post] Get Params From Body By Model
        /// 
        /// 此方法和 PostFromBody 一模一样，因为FromBody只能传一个参数
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromBodyByModel([FromBody]ArgModel arg)
        {
            return Ok(arg);
        }

        /// <summary>
        /// [Post] Get Params From Body
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromBodyInt([FromBody]int winCode=666)
        {
            return Ok(winCode);
        }

        /// <summary>
        /// [Post] Get Params From Body
        /// </summary>
        /// <returns></returns>
        /// <remarks>字符串参数记得加上双引号</remarks>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromBodyString([FromBody]string text="good boy")
        {
            return Ok(text);
        }
        /// <summary>
        /// [Post] Get Params From Form
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromForm([FromForm]int id = 233, [FromForm]string url = "http://www.example.com/q/2019/14323", [FromForm]bool isValid = true)
        {
            return Ok(new { id, url, isValid });
        }

        /// <summary>
        /// [Post] Get Params From Form  By Model
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromFormByModel([FromForm]ArgModel arg)
        {
            return Ok(arg);
        }

        /// <summary>
        /// [Post] Get Params From Header
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromHeader([FromHeader(Name = "accept-language")]string acceptLanguage, [FromHeader]string cookie,[FromHeader(Name = "accept-encoding")]string acceptEncoding)
        {
            return Ok(new { acceptLanguage, cookie, acceptEncoding });
        }

        //From Header 转换到一个模型里面似乎不可行
        // wait for later....


        /// <summary>
        /// [Post] Get Params From Services
        /// 
        /// 这玩意还不懂咋用，先这样吧
        /// </summary>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult PostFromServices([FromServices]int id = 233, [FromServices]string url = "http://www.example.com/q/2019/14323", [FromServices]bool isValid = true)
        {
            return Ok(new { id, url, isValid });
        }
    }
}